﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoShopping.Model;
using System.Data;

namespace GoShopping.BLL
{
    /// <summary>
    /// 商品管理员操作类
    /// </summary>
    public class ProductManagerBean
    {
        /// <summary>
        /// 获取商品管理员列表
        /// </summary>
        /// <returns></returns>
        public IList<ProductManager> GetProductManagerList()
        {
            IList<ProductManager> userList = new List<ProductManager>();
            DataTable dt = Databases.DBGoShopping.Select(GoShoppingNBear.ProductManager).ToDataSet().Tables[0];
            ProductManager[] userArr = NBear.Mapping.ObjectConvertor.ToArray<DataTable, ProductManager>(dt);

            foreach (ProductManager u in userArr)
            {
                userList.Add(u);
            }
            return userList;
        }

        /// <summary>
        ///  根据ID获取商品管理员
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public ProductManager GetOneProductManager(int userID)
        {
            DataTable dt = Databases.DBGoShopping.Select(GoShoppingNBear.ProductManager).Where(GoShoppingNBear.ProductManager.ID == userID).ToDataSet().Tables[0];
            ProductManager[] userArr = NBear.Mapping.ObjectConvertor.ToArray<DataTable, ProductManager>(dt);
            if (userArr.Length == 1)
            {
                return userArr[0];
            }
            else
            {
                throw (new Exception("ProductManager ID Is Not Single!"));
            }
        }

        /// <summary>
        ///  根据MembershipName获取商品管理员
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public ProductManager GetOneProductManagerByName(string userName)
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.ProductManager).Where(GoShoppingNBear.ProductManager.LoginName == userName).ToSingleObject<ProductManager>();
        }

        /// <summary>
        /// 更新商品管理员信息
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public bool UpdateProductManager(ProductManager u)
        {
            int i = Databases.DBGoShopping.Update(GoShoppingNBear.ProductManager).AddColumn(
                //GoShoppingNBear.ProductManager.Email, u.Email).AddColumn(
                GoShoppingNBear.ProductManager.LoginTime, u.LoginTime).AddColumn(
                //GoShoppingNBear.ProductManager.Password, u.Password).AddColumn(
                GoShoppingNBear.ProductManager.Phone, u.Phone).AddColumn(
                GoShoppingNBear.ProductManager.Sex, u.Sex).Where(GoShoppingNBear.ProductManager.ID == u.ID).Execute();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
